<?php

// 产品管理首页
class ProductAction extends Action
{

public function _initialize()
{
checkLoginSession();
}

public function index()
{
$pdtcodeDao = D("Product_cat_code");

//dump('pdt' . $_SESSION['UserInfo']['user_id']);
//DUMP(Think.session.UserInfo);
$pdtcodeList = $pdtcodeDao->findAll();


$this->assign('pdtcodeList', $pdtcodeList);

//dump($pdtcodeList);
$pdtDao = D("Product");
$tname = 'Product';
$Field = 'id_tb_product';
$Asc = ' ASC';

$searchwords = makeSearchWord();
//dump($searchwords);
$arrayVa = ShowTable($tname, $Field, $Asc, $searchwords);

$pdtList = $arrayVa['a'];
//dump($pdtList);
//dump($list);
$page = $arrayVa['b'];
//dump($page);
if (count($arrayVa['a']) < 1 or ($arrayVa['a'] == null))
{
$this->assign('empty', '没有要查询的记录');

}
//$pdtList = $pdtDao->findAll();
$this->assign('pdtList', $pdtList);

//$this->assign('content', 'Product:index');
$this->assign('page', $page);
$this->display('Product:index');

}

public function addPdtCodeForm()
{
$this->display('addPdtCodeForm');
}
public function initProductCode()
{
$pdtDao = D("Product_cat_code");
$data = array("cat_code" => strtoupper($_GET["cat_code"]), "describe" => $_GET["describe"]);
//"features" => $_GET["features"]);
if ($id_tb_product_cat_code = $pdtDao->add($data))
{
$error = 0;
$this->success('数据保存成功！');
}

}


public function editPdtCode()
{
if (!empty($_GET['id_tb_product_cat_code']))
{
$pdtcodeDao = D("Product_cat_code");
$userid = $_SESSION['UserInfo']['boss'];
$pdtcodeList = $pdtcodeDao->where('id_tb_product_cat_code=' . $_GET["id_tb_product_cat_code"] . " and userid = '$userid'")->
find();

if ($pdtcodeList)
{
$this->assign('vo', $pdtcodeList);
$this->display();
} else
{
exit('编辑项不存在！');
}


} else
{
exit('编辑项不存在！');
}

}


public function updatePdtCode()
{
$userid = $_SESSION['UserInfo']['boss'];
$PdtCode = M("Product_cat_code");
$data['cat_code'] = $_GET["cat_code"];
$data['describe'] = $_GET["describe"];
//$data['features'] = $_GET["features"];
if ($PdtCode->where('id_tb_product_cat_code=' . $_GET["id_tb_product_cat_code"]. " and userid = '$userid'")->
save($data))
$this->success('数据修改成功！');
}

public function delPdtCode()
{

$PdtCode = M("Product_cat_code");
$userid = $_SESSION['UserInfo']['boss'];
if (!empty($_GET["id_tb_product_cat_code"]))
{
$result = $PdtCode->where('id_tb_product_cat_code=' . $_GET["id_tb_product_cat_code"] ." and userid = '$userid'")->
delete();
if (false !== $result)
{
//$this->ajaxReturn($_GET["id_tb_product_cat_code"],'删除成功！',1);
$this->assign('waitSecond', 1);
$this->success('删除成功');

} else
{
$this->error('删除出错！');
}
} else
{
$this->error('删除项不存在！');
}
}

public function addPdtForm()
{

$pdtCodeDao = D("Product_cat_code");
$pdtcodeList = $pdtCodeDao->findAll();
$this->assign('pdtcodeList', $pdtcodeList);
$this->display('addPdtForm');
}


public function editPdt()
{
if (!empty($_GET['id_tb_product']))
{
$Pdt = M("Product");
$vo = $Pdt->where('id_tb_product=' . $_GET["id_tb_product"])->find();
if ($vo)
{
$this->assign('vo', $vo);

//$tmp = showChk($vo["features"]);

//$this->assign('featuresBox', $tmp);
//dump($tmp);
$this->display();
} else
{
exit('编辑项不存在！');
}


} else
{
exit('编辑项不存在！');
}

}
public function updatePdt()
{
$Pdt = M("Product");
$data['id_tb_product'] = $_GET["id_tb_product"];
$data['product_name'] = $_GET["product_name"];
$data['pic'] = $_GET["pic"];
$data['unit'] = $_GET["unit"];
$data['weight'] = $_GET["weight"];
$data['supplier'] = $_GET["supplier"];
//$data['features'] = $_GET["features"];
$data['price'] = $_GET["price"];
$data['in_price'] = $_GET["in_price"];
if ($Pdt->where('id_tb_product=' . $_GET["id_tb_product"])->save($data))
$this->success('数据修改成功！');
}

public function delPdt()
{

$Pdt = M("Product");
$userid = $_SESSION['UserInfo']['boss'];
if (!empty($_GET["id_tb_product"]))
{
$result = $Pdt->where('id_tb_product=' . $_GET["id_tb_product"] . " and userid = '$userid'")->delete();
if (false !== $result)
{
$this->assign('waitSecond', 1);
$this->success('删除成功');

} else
{
$this->error('删除出错！');
}
} else
{
$this->error('删除项不存在！');
}
}

public function addPdt()
{
    if (!empty($_FILES))
    {
    $this->_addPdt();
    }
}

// 文件上传，商品信息和库存初始化
protected function _addPdt()
{
import("@.ORG.UploadFile");
$upload = new UploadFile();
$upload->maxSize = 3292200;
$upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
$pth = __ROOT__ . '/Public/Uploads/' . $_SESSION['UserInfo']['boss'] . '/';
$upload->savePath = $pth;
$upload->thumb = true;
$upload->thumbPrefix = 's_'; //生产2张缩略图
$upload->thumbMaxWidth = '120,60';
$upload->thumbMaxHeight = '140,60';
$upload->saveRule = uniqid();
//删除原图
$upload->thumbRemoveOrigin = true;

if (!$upload->upload())
{
$this->error($upload->getErrorMsg());
} else
{
$uploadList = $upload->getUploadFileInfo();
$_img = $uploadList[0]['savename'];
}

//初始化商品信息和数量
$pdtDao = D("Product");
$catcodeDao = D("Product_cat_code");

if (empty($_POST['simple_code']))
{
$this->error('编码必须！');
} elseif (empty($_POST['weight']))
{
$this->error('重量必须！');
} elseif (empty($_POST['product_name']))
{
$this->error('商品名必须！');
}

$tpcode = $_POST['simple_code'];
$userid = $_SESSION['UserInfo']['boss'];
if (strlen($tpcode) !== SIMPLE_CODE_LENGTH)
{
$this->error('编码长度不对!' . SIMPLE_CODE_LENGTH);
}
$codeex = $pdtDao->where('simple_code =' . '"' . $tpcode . '"' . " and userid = '$userid'")->find();
if ($codeex)
{
$this->error('编码已经存在!');
}
$userid = $_SESSION["UserInfo"]['boss'];
$data = array("simple_code" => strtoupper($_POST["simple_code"]), "product_name" =>
$_POST["product_name"], "pic" => $_img, "weight" => $_POST["weight"],
 "userid" => $userid);
//"features" => $_POST["features"],"unit" => $_POST["unit"], 
//"supplier" => $_POST["supplier"], "price" => $_POST["price"],"in_price" => $_POST["in_price"],
if ($id_tb_product = $pdtDao->add($data))
{
$error = 0;
$catstr = substr($_POST["simple_code"], 0, 1);
$rtt = $catcodeDao->execute("UPDATE `tb_product_cat_code` SET `counter` = counter + 1 WHERE cat_code = '$catstr' and userid = '$userid'" );
    if ($rtt)
    { if(isset($_POST['init_count']) and isset($_POST['init_loc']) and exitLoccode($_POST['init_loc']))
        {$dpPdo = D('Depot_product');$init_count = $_POST['init_count'];$spcode = $_POST["simple_code"];
             $loc = $_POST['init_loc'];
            $trr = $dpPdo->execute("INSERT INTO `tb_depot_product` (`simple_code`,`real_count`,`userid`) VALUES ('$spcode','$init_count','$userid')");
            if($trr)
            {   
               $lccPdo = D('Loc_code_count');
               $trrt =  $lccPdo->execute("INSERT INTO `tb_loc_code_count` (`simple_code`,`location_code`,`count`,`userid`) VALUES ('$spcode','$loc','$init_count','$userid')");
               if($trrt)
               {
                $this->success('数据保存成功！');
               }
               else
               {
                $this->error('数据保存失败！');
               } 
            }
            else
            {
                $this->error('数据保存失败！');            
            }         
        }
        else
        {
            if(isset($_POST['init_count']) and isset($_POST['init_loc']))
            $this->error('添加失败,数据有错误!！'); //有初始化数量和位置码，但位置码不存在  
        }

    } else
    {
    $this->error('添加失败,数据有错误!');
    }
} 
else
{
$this->error('数据保存失败!');
}

}

public function checkCL(){//检查简码和位置码是否存在,如果有数量参数，则写入session记录，作为入库记录
$rt1 =false;$rt2 = false;
if($_POST['code'])
    {
    if(exitDpcode($_POST['code'])){$rt1=true;}
    else{$rt1=false;}
    }  
if($_POST['loc'])
    {
    if(exitLoccode($_POST['loc'])){$rt2=true;}
    else{$rt2=false;}
    }
$data = array('codeChk'=>$rt1,'locChk'=>$rt2);  
  
if(($_POST['count']) AND $rt1 AND $rt2 )//如果有数量参数，且简码，位置码都存在，写入入库记录
    {
    addinRecx($_POST['code'],$_POST['count'],$_POST['loc']);
    echo ajaxrt('ok','入库记录已保存',$data);
    EXIT();
    }   
//非全部存在，或不是入库验证，是其他验证情况的，返回对应验证结果（在data里面）
 echo ajaxrt('normal','验证参考结果数据',$data); EXIT();     
}//end checkCL



}

?>